package com.ynwa.maths.sort;

import java.util.Arrays;
import java.util.Random;

/**
 * @author gzd
 * @date 2018-10-22 下午 2:53
 * @desc 插入排序
 */
public class InsertionSort {

    public static void main(String[] args) {
        int[] nums = new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
        Random random = new Random();

//        for (int i = 0; i < 10; i++) {
//            nums[i] = random.nextInt(10);
//        }

        System.out.println("初始数组值: " + Arrays.toString(nums));

        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            int j = i - 1;
            for (; j >= 0; --j) {
                if (nums[j] > num) {
                    nums[j + 1] = nums[j];
                } else {
                    break;
                }
            }

            nums[j + 1] = num;
            System.out.println("过程数组值: " + Arrays.toString(nums));

        }

        System.out.println("排序数组值: " + Arrays.toString(nums));
    }
}
